/**
 * AppUtils.java
 *
 * @version 1.0
 * @author nicky.han
 */
package cn.etgps.etgps.utils;

import android.content.Context;
import android.widget.Toast;

import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import cn.etgps.etgps.R;

/**
 * Support tool functions
 *
 * @author nicky.han
 */
public class AppUtils {
    /**
     * View for show some messages.
     */
    private static Toast mToast = null;

    /**
     * 根据date得到年月日
     */
    public static String getCurrentDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return sdf.format(date);
    }

    /**
     * 根据date得到小时分钟
     */
    public static String getCurrentTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.CHINA);
        return sdf.format(date);
    }

    public static String doURLEncode(String param) {
        try {
            return URLEncoder.encode(param, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return param;
    }

    /**
     * 获取当前日期
     */
    public static Calendar getCurrentSystemDate() {
        Date nowTime = new Date(System.currentTimeMillis());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowTime);
        return calendar;
    }

    /**
     * 指定时间格式获取当前时间
     */
    public static String getCurrentTime(String format) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        String currentTime = sdf.format(date);
        return currentTime;
    }

    /**
     * 去掉秒的时间
     */
    public static String reFormatTimeWithoutSeconds(String time) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(time);
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String currentTime = sdf.format(date);
            return currentTime;
        } catch (Exception e) {
            e.printStackTrace();
            return time;
        }
    }

    /**
     * 获取当前时间字符串
     */
    public static String getCurrentTime() {
        return getCurrentTime("yyyy-MM-dd  HH:mm:ss");
    }

    /**
     * 得到方向
     */
    public static String getDirectionString(int direction) {
        if (direction < 23 || direction > 338) {
            return "北";
        } else if (direction < 68) {
            return "东北";
        } else if (direction < 113) {
            return "东";
        } else if (direction < 157) {
            return "东南";
        } else if (direction < 203) {
            return "南";
        } else if (direction < 248) {
            return "西南";
        } else if (direction < 293) {
            return "西";
        } else {
            return "西北";
        }
    }

    /**
     * 得到方向
     */
    public static int getDirectionMarkerId(int direction) {
        if (direction < 23 || direction > 338) {
            return R.drawable.icon_car_north;
        } else if (direction < 68) {
            return R.drawable.icon_car_north_east;
        } else if (direction < 113) {
            return R.drawable.icon_car_east;
        } else if (direction < 157) {
            return R.drawable.icon_car_south_east;
        } else if (direction < 203) {
            return R.drawable.icon_car_south;
        } else if (direction < 248) {
            return R.drawable.icon_car_south_west;
        } else if (direction < 293) {
            return R.drawable.icon_car_west;
        } else {
            return R.drawable.icon_car_north_east;
        }
    }

    /**
     * Show toast using the message and duration.
     *
     * @param message  the message shown in toast.
     * @param duration the time for which the toast will be shown.
     */
    public static void showToastMessage(Context context, final String message, int duration) {
        try {
            if (mToast == null) {
                mToast = Toast.makeText(context, message, duration);
            } else {
                mToast.setText(message);
                mToast.setDuration(duration);
            }
            mToast.show();
        } catch (Exception e) {
            return;
        }
    }

    /**
     * 转换分钟的为天数和小时
     */
    public static String getStopTime(String stoptime) {
        float timef = Float.valueOf(stoptime);
        int time = (int) timef;
        String tim = "";
        if (time > 0) {
            int day = time / 1440;
            int hour = time % 1440 / 60;
            int minte = time % 1440 % 60;
            if (day > 0) {
                tim = day + "天 ";
            }
            if (hour > 0) {
                tim += (String.valueOf(hour) + "小时 ");
            }
            if (minte > 0) {
                tim += (String.valueOf(minte) + "分钟");
            }
            return tim;
        }
        return stoptime;
    }

    public static void setPath() {
        Constant.URI_GET_TRUCK_IN_GARAGE = Constant.URI_BASH_PATH + "GetVehicleInfo.ashx?";
        Constant.URL_GET_TRUCK_LOGIN = Constant.URI_BASH_PATH + "LoginForComAndPer.ashx?";
        Constant.URL_GET_TRUCK_SEARCH_DETAIL = Constant.URI_BASH_PATH + "GetVehicleDetailedInfo.ashx?";
        Constant.URL_GET_TRUCK_PLAY_BACK = Constant.URI_BASH_PATH + "Playback.ashx?";
        Constant.URL_CHANGE_PWD = Constant.URI_BASH_PATH + "ChangePwd.ashx?";
        Constant.URL_SAVE_FEEDBACK = Constant.URI_BASH_PATH + "SaveFeedback.ashx";
    }

    public static Date getDate(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d = null;
        try {
            d = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
}
